package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.LeafletServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Leaflet;
import com.hxkj.domain.po.WebsiteAddr;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.hxkj.client.service.taglib.config.LableConstant.CMS_LEAFLETS;

/**
 * 单页列表 自定义标签
 *
 * @author chenfangzhou 20185/09/14
 */
@Component
@Slf4j
public class LeafletDirective extends AbstractDirective {
    @Autowired
    private LeafletServiceClient leafletServiceClient;

    /**
     * 站点ID。
     */
    private static final String SITE_ID = "siteId";
    /**
     * 单页id集合,可传多个用,分隔。
     */
    public static final String LEAFLET_IDS = "leafletIds";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            String leafletIds = getString(LEAFLET_IDS, params);
            String note = super.getNote(env);
            List<Leaflet> leaflets;
            if (StringUtils.isBlank(leafletIds)) {
                log.debug("单页id为空!");
                leaflets = new ArrayList<>();
            } else {
                Leaflet leaflet = new Leaflet();
                leaflet.setSiteId(siteId);
                leaflet.setLeafletIds(leafletIds);

                //单页详情
                leaflets = leafletServiceClient.getLeafletByIds(leaflet).getData();
                WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);
                if (StringUtils.isNotBlank(note)) {
                    for (Leaflet leaflet_ : leaflets) {
                        leaflet_.setManuscriptPath(webSiteAddr.getIpAdd() + leaflet_.getManuscriptPath());
                    }

                }else{
                    // 单页静态文件链接加上下文
                    Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                    Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                    for (Leaflet leaflet_ : leaflets) {
                        String urlContext = siteCtxMap.get(siteId);
                        if(StringUtils.isNotBlank(urlContext)) {
                            leaflet_.setManuscriptPath(urlContext + leaflet_.getManuscriptPath());
                        }
                    }
                }
            }

            env.setVariable("leaflets", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(leaflets));
            String userToken = super.getUserToken(env);
            if (body != null) {
                if (StringUtils.isEmpty(note)) {
                    body.render(env.getOut());
                } else {
                    Map<String, String> maps = new LinkedHashMap<>();

                    maps.put(CMS_LEAFLETS, "");
                    maps.put(SITE_ID, String.valueOf(getLong(SITE_ID, params)));
                    maps.put(LEAFLET_IDS, getString(LEAFLET_IDS, params));
                    try {
                        super.print(body, maps, env, userToken, params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                log.debug("单页列表所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
